package com.car.advertising.activity.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.car.advertising.R;
import com.car.advertising.entity.bean.RegisterBean;
import com.car.advertising.listener.SaveEditListener;
import com.car.advertising.view.EditTextDeleteView;
import com.support.widget.interfacelistener.OnRecyclerViewItemClickListener;
import com.support.widget.interfacelistener.OnRecyclerViewItemLongClickListener;
import com.support.widget.layout.adapter.BaseRecyclerViewAdapter;
import com.support.widget.layout.holder.BaseViewHolder;

import java.util.List;

/**
 * 开发者：LuoYi
 * Time: 2017 19:10 2017/5/3 05
 */

public class RegsiterAdapter extends BaseRecyclerViewAdapter<RegisterBean, BaseViewHolder> {

    private final int REGSITER_INPUT = 0;
    private final int REGSITER_COMMIT = 1;

    public RegsiterAdapter(Context context, List<RegisterBean> strings) {
        super(context, strings);
    }

    @Override
    public int getItemViewType(int position) {
        if (8 == position) {
            return REGSITER_COMMIT;
        }
        return REGSITER_INPUT;
    }

    @Override
    protected void bindView(BaseViewHolder holder, int position, RegisterBean model) {
        if (holder instanceof RegsiterHolder) {
            RegsiterHolder regsiterHolder = (RegsiterHolder) holder;
            regsiterHolder.nacigationTv.setText(model.getrNavigation());
            if (2 == model.getrId()) {
                regsiterHolder.contentEt.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            } else if (3 == model.getrId()) {
                regsiterHolder.contentEt.setInputType(InputType.TYPE_CLASS_PHONE);
            } else {
                regsiterHolder.contentEt.setInputType(InputType.TYPE_CLASS_TEXT);
            }
            regsiterHolder.contentEt.setHint(model.getrContentHint());
            regsiterHolder.contentEt.addTextChangedListener(new RegisterTextWatcher(regsiterHolder.contentEt));
            //通过设置tag，防止position紊乱
            regsiterHolder.contentEt.setTag(position);
        } else if (holder instanceof RegsiterCommitHolder) {
            RegsiterCommitHolder regsiterCommitHolder = (RegsiterCommitHolder) holder;
            regsiterCommitHolder.commitTv.setText(model.getrNavigation());
        }
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == REGSITER_INPUT) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_regsiter_input, parent, false);
            return new RegsiterHolder(mContext, view, this);
        } else {
            View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_regsiter_commit, parent, false);
            return new RegsiterCommitHolder(mContext, inflate, this, getClickListener(), getLongClickListener());
        }
    }

    class RegsiterHolder extends BaseViewHolder {

        public TextView nacigationTv;
        public EditTextDeleteView contentEt;

        public RegsiterHolder(Context context, View rootView, BaseRecyclerViewAdapter mBaseRecyclerViewAdapter) {
            super(context, rootView, mBaseRecyclerViewAdapter);
            nacigationTv = (TextView) rootView.findViewById(R.id.register_navigation_tv);
            contentEt = (EditTextDeleteView) rootView.findViewById(R.id.register_content_et);
        }
    }

    class RegsiterCommitHolder extends BaseViewHolder implements OnRecyclerViewItemClickListener, OnRecyclerViewItemLongClickListener {

        public OnRecyclerViewItemClickListener mListener;
        public OnRecyclerViewItemLongClickListener mLongClickListener;
        public TextView commitTv;

        public RegsiterCommitHolder(Context context, View rootView, BaseRecyclerViewAdapter mBaseRecyclerViewAdapter, OnRecyclerViewItemClickListener itemClickListener, OnRecyclerViewItemLongClickListener itemLongClickListener) {
            super(context, rootView, mBaseRecyclerViewAdapter, itemClickListener, itemLongClickListener);
            commitTv = (TextView) rootView.findViewById(R.id.register_commit_tv);
            this.mListener = itemClickListener;
            this.mLongClickListener = itemLongClickListener;
            commitTv.setOnClickListener(this);
            commitTv.setOnLongClickListener(this);
        }

        @Override
        public void onItemClick(ViewGroup parent, View view, int position) {
            if (mListener != null) {
                mListener.onItemClick(parent, view, position);
            }
        }

        @Override
        public void onItemLongClick(ViewGroup parent, View view, int position) {
            if (mLongClickListener != null) {
                mLongClickListener.onItemLongClick(parent, view, position);
            }
        }
    }

    class RegisterTextWatcher implements TextWatcher {

        private EditText editText;

        public RegisterTextWatcher(EditText editText) {
            this.editText = editText;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            //用户输入完毕后，处理输入数据，回调给主界面处理
            SaveEditListener listener = (SaveEditListener) mContext;
            if (s != null) {
                listener.getEditPositionText(Integer.parseInt(editText.getTag().toString()), s.toString());
            }
        }
    }
}
